// pages/ImproveInformation/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user_id : '',
    model:{
      username:'', //用户名
      sex:'', //性别
      area:'',  //小区
      addr_detail: '' //详细地址
    },
    sexShow:false,
    actions: [
      {
        name: '男',
      },
      {
        name: '女',
      }
    ],
    areaShow:false,
    areaList: [], //小区列表
    fileList: [],
    headUrl: '', //头像地址
    imageUrl:'',
    currentArea: [], //小区拷贝
    value:'',
    community_id:'', //小区id
    pageNum: 1,
    nomore: true
  },
  selectedSex(){
    this.setData({
      sexShow:true
    })
  },
  //性别选择
  onSelect(e){
    console.dir(e)
    this.setData({
      sexShow: false
    })
    let sex = 'model.sex'
    this.setData(
      { [sex]: e.detail.name}
    )
  },
  selectedArea(e){
    this.setData({
      areaShow: true
    })
  },
  onCancel(){
    this.setData({
      areaShow:false
    })
  },
  inputChange(e){
    
  },

  //提交表单
  async formSubmit(e){
    let that = this;
    const { username, sex, addr_detail} = e.detail.value
    let newSex = sex=='男' ? 1 : 2
    const { community_id, headUrl } = this.data
    if(!username){Toast('请输入姓名');return;}
    if(!sex){Toast('请选择性别');return;}
    if(!community_id){Toast('请选择小区');return;}
    if(!addr_detail){Toast('请选择地址');return;}

      const { data: res } = await api('POST', 'Recycle/user/addUserInfo', {
        user_id:that.data.user_id,
        headUrl,
        user_name: username,
        sex: newSex,
        community_id,
        addr_detail
      })
      if(res.code == 200){
        Toast(res.msg)
        wx.setStorageSync('token', res.data.token)
        wx.setStorageSync('userInfo', res.data)
        wx.switchTab({
          url: '../index/index',
        })
        wx.removeStorageSync('needInfo')
      }
  },
  //头像上传
  afterRead(event) {

    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: app.globalData.upfiles + 'Recycle/other/imageUpload', // 仅为示例，非真实的接口地址
      filePath: file.path,
      name: 'file',
      formData: {file },
      success:(res) =>{
        console.log(res.data)
        let fileList = []
        let result = JSON.parse(res.data)
        
        // 上传完成需要更新 fileList
        if (result.code ===200){
          fileList.push({ name: '图片1', url: app.globalData.imageUrl+ result.data });
          this.setData({
            headUrl: result.data,
            fileList
          })
        }
      
      },
    });
  },
  delImag(e){
    console.dir(e)
    this.setData({
      fileList:[]
    })
  },
  async getAllareaList() {
    let { pageNum, areaList } = this.data
    const res = await api('GET', 'Recycle/area/getCommunities', {
      pageNum
    })
    if (res.data.code === 200) {
      if (res.data.data.length < 10) {
        this.setData({
          nomore: false
        })
      }
      this.setData({
        areaList: areaList.concat(res.data.data),
      })
    }
  },
  loadMore() {
    const { pageNum } = this.data
    this.setData({
      pageNum: pageNum + 1
    })
    this.getAllareaList()
  },
  onClose(){
    this.setData({
      areaShow:false
    })
  },
  chooseArea(e) {
    const area = 'model.area'
    let id = e.currentTarget.dataset.id
    this.data.areaList.forEach(i => {
      if (id == i.community_id) {
        this.setData({
          [area]: i.community_name,
          areaShow: false,
          community_id: id
        })
      }
    })
  },
  async searchArea(e) {
    const value = e.detail
    const res = await api('GET', 'Recycle/area/getCommunities', {
      pageNum: 1,
      key: value
    })
    if (res.data.code === 200) {
      if (res.data.data && res.data.data.length < 10) {
        this.setData({
          nomore: false
        })
      }
      this.setData({
        areaList: res.data.data,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.error(options);
    that.setData({
      user_id : options.user_id,
      imageUrl: app.globalData.imageUrl
    })
    this.getAllareaList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})